using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CodeStory.Core.UI;
using CodeStory.Core.Attributes;
using System.ComponentModel;
using CodeStory.News.ObjectEntity;
using Activa.CoolStorage;
using CodeStory.WebControls;

namespace CodeStory.WebApplication.Modules.News
{
	public partial class NewsList : Portlet
	{
		protected override void OnInit(EventArgs e) {
			base.OnInit(e);
			Load += new EventHandler(NewsList_Load);
		}

		void NewsList_Load(object sender, EventArgs e) {
			//throw new Exception("The method or operation is not implemented.");
			if (!IsPostBack) {
				BindNews();
			}
		}

		private void BindNews() {
			CSList<NewsContentObject> list;
			if (NewsCategoryID.Equals(Guid.Empty)) {
				list = NewsContentObject.List().LimitTo(MaxNewsNumber);
			} else {
				list = NewsContentObject.List("CategoryID=@CategoryID","@CategoryID",NewsCategoryID).LimitTo(MaxNewsNumber);
			}
			rptNews.DataSource = list;
			rptNews.DataBind();
		}

		[PortletSetting]
		[Description("")]
		[PropertyEditor(typeof(NewsCategoryEditor))]
		public Guid NewsCategoryID {
			get {
				object obj = Setting["NewsCategoryID"];
				return obj == null ? Guid.Empty : (Guid)obj;
			}
			set { Setting["NewsCategoryID"] = value; }
		}

		[PortletSetting]
		[Description("")]
		public int MaxNewsNumber {
			get {
				object obj = Setting["MaxNewsNumber"];
				return obj == null ? 10 : (int)obj;
			}
			set { Setting["MaxNewsNumber"] = value; }
		}

		internal class NewsCategoryEditor : DropDownPropertyEditor{
			public override void Prepare() {
				//base.Prepare();
				Items.Clear();
				ListItem all = new ListItem("all",Guid.Empty.ToString());
				Items.Add(all);
				CSList<NewsCategoryObject> list = NewsCategoryObject.List();
				foreach (NewsCategoryObject obj in list) {
					ListItem item = new ListItem(obj.CategoryName,obj.CategoryID.ToString());
					Items.Add(item);
				}
			}
		}
	}
}